<?php
/**
 * Created by PhpStorm.
 * User: zouyanan
 * Date: 2019/5/11
 * Time: 下午11:04
 */

namespace app\common\model;


use think\Model;

class UserMini extends Model
{
    const IS_REGISTER = 1;
    const IS_NOT_REGISTER = 0;
    protected $table = 'fa_user_mini';

    /**
     * 保存openid
     * @param $openId
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function saveOpenid($openId){
        $item = self::where('openid',$openId)->find();
        if (empty($item)){
            $obj = new self();
            $obj->openid = $openId;
            $obj->token = md5($openId);
            $obj->create_time = date('Y-m-d H:i:s');
            $obj->update_time = date('Y-m-d H:i:s');
            $obj->save();
        }
        return true;
    }

    /**
     * 用户注册
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function register($param){
        $user = self::where('token',$param['token'])->find();
        if(empty($user)){
            return ['ret'=>false,'message'=>'用户不存在'];
        }
        if ($user->is_register == self::IS_REGISTER){
            return ['ret'=>false,'message'=>'用户已注册'];
        }
        $mobileUser = self::where('mobile',$param['mobile'])->find();
        if (!empty($mobileUser)){
            return ['ret'=>false,'message'=>'手机号已注册'];
        }
        $user->name = $param['name'];
        $user->mobile = $param['mobile'];
        $user->password = md5($param['password']);
        $user->avatar = $param['avatar'];
        $user->is_register = self::IS_REGISTER;
        $user->save();
        return ['ret'=>true,'message'=>'success'];
    }

    /**
     * 登陆
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function login($param){
        $user = self::where('mobile',$param['mobile'])->find();
        if (empty($user)){
            return ['ret'=>false,'message'=>'手机号未注册'];
        }
        if (!empty($param['company_name'])){
            $user->company_name = $param['company_name'];
            $user->save();
        }
        if (!$param['code'] && ($user->password != md5($param['password']))) {
            return ['ret'=>false,'message'=>'用户名或密码错误'];
        }
        return ['ret'=>true,'message'=>'登陆成功','data'=>$user->token];
    }

    /**
     * 检查是否静默授权
     * @param $token
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function checkAuth($token){
        $user = self::where('token',$token)->find();
        if (empty($user)){
            return ['ret'=>false,'message'=>'用户未授权'];
        }else{
            return ['ret'=>true,'data'=>$user];
        }
    }

    /**
     * 检查是否登陆
     * @param $token
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function checkLogin($token){
        $user = self::where('token',$token)->where('is_register',self::IS_REGISTER)->find();
        if (empty($user)){
            return ['ret'=>false,'message'=>'用户未登陆'];
        }else{
            return ['ret'=>true,'data'=>$user];
        }
    }
}